﻿using System;
using System.Collections.Generic;
using System.Text;
using BlueFinity.mvNET.CoreObjects;

namespace mvLibrary.Helpers.Utils
{
    public static class FenkUtils
    {
        private const char PICK_SEPARATOR = 'ý';
        public const int ZERO = 0;
        private static IDictionary<string, string> separatorsByType = new Dictionary<string, string>(){{"PDF",DataBASIC.AM},{"HTM","<br>"}};

        public static string ToFenkFormat(DateTime date)
        {
            return date.ToString("MM/dd/yy");
        }

        public static string ConcatAttributesToGetMultivalue(string firstAttribute, params string[] restAttributes)
        {
            StringBuilder multivalueAttribute = new StringBuilder(firstAttribute);

            foreach (var attribute in restAttributes)
            {
                if (!string.IsNullOrEmpty(attribute))
                {
                    multivalueAttribute.Append(PICK_SEPARATOR);
                    multivalueAttribute.Append(attribute);
                }
            }

            return multivalueAttribute.ToString();
        }

        public static string[] SplitMultivalueAttributes(string multiValueAttribute)
        {
            return multiValueAttribute.Split(PICK_SEPARATOR);
        }
        
        public static string PrintFromLine(string imprimirDesde,string report,string reportType ="PDF")
        {
            int apartirDeLaLinea;
            int.TryParse(imprimirDesde, out apartirDeLaLinea);

            if (apartirDeLaLinea != 0)
            {
                for (int index = 0; index <= apartirDeLaLinea; index++)
                {
                    report = separatorsByType[reportType] + report;
                }
            }
            return report;
        }
        
        public static bool EsUsuarioPrincipal(string userId)
        {
            return userId.Equals("PNL") || userId.Equals("RL") || userId.Equals("GB") || userId.Equals("SGS") || userId.Equals("HA");
        }

        public static int GetIndexFromAttribute(object attribute, string elementToFind)
        {
           string[] valoresSeparados = SplitMultivalueAttributes(attribute.ToString());
            for (int index = 0; index < valoresSeparados.Length; index++)
            {
                if (valoresSeparados[index].Equals(elementToFind))
                    return index;
            }

            return 0;
        }

        public static bool GetBoolFromYesNoString(string permisoString)
        {
            return permisoString.Equals("S") || permisoString.Equals("SI");
        }

        internal static IDictionary<string,string> GetBasicListIdValue(mvItemList mvList)
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            for (int index = 1; index <= mvList.Count; ++index)
            {
                if (!(mvList.EOL))
                {
                    mvList.ReadNextID();
                    dict.Add(mvList.CurrentItem.ID, mvList.CurrentItem.getData(1).ToString());
                }
            }

            return dict;
        }

    }
}
